package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2022-01-19 00:30
 **/
public class LeetCode219 {

    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < nums.length; i++) {
            //移除左边窗口元素
            if (i > k) {
                set.remove(nums[i - k - 1]);
            }
            //存在重复元素,返回true
            if (!set.add(nums[i])) {
                return true;
            }
        }

        return false;
    }

}
